﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.Car;

namespace S4Store.Controllers
{
    public class PinpaiController : BaseController
    {

        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            PinpaiListPage model = new PinpaiListPage();
            return View(model);            
        }

        public ActionResult Query()
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                var data = CarService.GetPinPaiQuery().ToList();
                return View(data);
            }
        }

        public ActionResult Select()
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                var data = CarService.GetPinPaiQuery().ToList();
                return View(data);
            }
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Add(PinPai model)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                CarService.AddPinpai(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success=true
                    }
                };
            } 
        }

        [HttpPost]
        public ActionResult Modify(int id)
        {
            using (var t = new Transtion())
            {
                t.Inject<CarService>(CarService);
                var model = CarService.GetPinPaiQuery().FirstOrDefault(x => x.Id == id);
                t.Commit();
                if (model != null)
                {
                    return View(model);
                }
                return this.Content("品牌不存在");
            }   
            
        }


        [HttpPost]
        public JsonResult ModifyBack(PinPai model)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                CarService.ModifyPinpai(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }

        }

        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            CheckPower(Position.SystemManager);
            if(Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    CarService.DeleteCarBrand(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Upline(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要上线的品牌");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    var model = CarService.GetPinPaiQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = false;
                        CarService.ModifyPinpai(model);
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "操作成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Downline(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要下线的品牌");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    var model = CarService.GetPinPaiQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = true;
                        CarService.ModifyPinpai(model);
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "操作成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }
}
